﻿Imports Equipment.Core
Imports Equipment.DataAccess
Imports Equipment.Business
Imports Equipment.Model
Imports log4net

Namespace Equipment
    <RequiresPageAcces(Page:=PageType.SitesDepartmentSetup)> _
    Public Class SitesDepartmentSetupController
        Inherits System.Web.Mvc.Controller

        Private Shared logger As ILog = LogManager.GetLogger(GetType(SitesDepartmentSetupController))

        Function Index() As ActionResult
            Dim timeZones As Dictionary(Of String, String) = SystemHelper.GetTimeZones()

            Dim sites As IEnumerable(Of SiteDepartment) = SiteDepartmentManager.GetAllSitesFor(Application.CurrentUserInfo)
            ViewData("DDSiteList") = New SelectList(sites, "ID", "Description")
            ViewData("SiteList") = sites

            If sites.Count() > 0 Then
                Dim currSite = sites(0)
                ViewData("DepartmentList") = SiteDepartmentManager.RetrieveAllDepartmentsBySite(currSite.ID)
                ViewData("SelectedSiteID") = currSite.ID
            Else
                ViewData("SelectedSiteID") = 0
            End If
            ViewData("TimeZone") = New SelectList(timeZones, "Key", "Value")

            ViewData("Holidays") = Holiday.RetrieveAll()
            ViewData("EntityType") = EntityType.SiteDepartment

            If Application.CurrentUserInfo.IsSuperUser Then
                ViewData("IsSuperUser") = True
            End If

            Return View()
        End Function

#Region "AJAX"


        <HttpPost()> _
        Function CheckDepartmentExists(ByVal siteID As Integer, ByVal curDepID As Integer, ByVal depDesc As String) As JsonResult
            Dim actionResult As New JSONActionResult
            actionResult.Result = Result.SUCCESS
            Try
                Dim dep = SiteDepartment.GetDepartmentByNameForSiteExcept(siteID, curDepID, depDesc)

                If dep IsNot Nothing Then
                    actionResult.Result = Result.ERROR
                    actionResult.Message = "This Department already exists"
                End If

            Catch ex As Exception
                actionResult.Result = Result.ERROR
                actionResult.Message = ex.Message
            End Try

            Return Json(actionResult)
        End Function

        Function SearchDepartmentByName(ByVal q As String) As ActionResult
            Dim query = SiteDepartmentManager.SearchDepartmentByNameFor(Application.CurrentUserInfo, q)
            Dim result = DepartmentWrapper.ConvertToWrapperList(query)
            Return Json(result.ToArray(), JsonRequestBehavior.AllowGet)
        End Function

        <HttpPost()> _
        Function AddSite() As ActionResult
            SiteDepartment.AddSite()
            ViewData("SiteList") = SiteDepartmentManager.GetTreeFor(Application.CurrentUserInfo)
            Return PartialView("~/Views/SitesDepartmentSetup/Controls/SiteDepartmentTree.ascx", ViewData("SiteList"))
        End Function

        <HttpPost()> _
        Function AddDepartment(ByVal siteID As Integer) As ActionResult
            SiteDepartment.AddDepartment(siteID)
            ViewData("SiteList") = SiteDepartmentManager.GetTreeFor(Application.CurrentUserInfo)
            Return PartialView("~/Views/SitesDepartmentSetup/Controls/SiteDepartmentTree.ascx", ViewData("SiteList"))
        End Function

        <HttpPost()> _
        Function SetSiteFilter(ByVal filter As String) As ActionResult
            Dim sites As IEnumerable(Of SiteDepartment) = SiteDepartmentManager.GetTreeFor(Application.CurrentUserInfo, filter)
            ViewData("DDSiteList") = New SelectList(sites, "ID", "Description")
            Return PartialView("~/Views/SitesDepartmentSetup/Controls/SiteDropDown.ascx", ViewData("DDSiteList"))
        End Function

        <HttpPost()> _
        Function SetFilter(ByVal filter As String) As ActionResult
            ViewData("SiteList") = SiteDepartmentManager.GetTreeFor(Application.CurrentUserInfo, filter)
            Return PartialView("~/Views/SitesDepartmentSetup/Controls/SiteGrid.ascx", ViewData("SiteList"))
        End Function

        <HttpPost()> _
        Function SaveSite(ByVal id As Integer, ByVal desc As String, ByVal addr As String, ByVal isActive As Boolean, ByVal timeZoneID As String, ByVal filter As String) As ActionResult
            Dim site As SiteDepartment
            If id > 0 Then
                site = SiteDepartment.GetByPrimaryKey(id)
                If site IsNot Nothing Then
                    Dim changeDetails As New List(Of String)
                    Dim oldDesc As String = site.Description
                    If site.Description <> desc Then
                        changeDetails.Add(String.Format(Constants.UI.FIELD_MODIFICATION_TEMPLATE, "Name", site.Description, desc))
                    End If
                    If site.IsActive <> isActive Then
                        changeDetails.Add(String.Format(Constants.UI.FIELD_MODIFICATION_TEMPLATE, "Status", site.IsActive.ToString(), isActive.ToString()))
                    End If
                    If site.Address <> addr Then
                        changeDetails.Add(String.Format(Constants.UI.FIELD_MODIFICATION_TEMPLATE, "Address", site.Address, addr))
                    End If
                    site.Description = desc
                    site.Address = addr
                    site.IsActive = isActive
                    site.TimeZoneID = timeZoneID
                    site.Save()
                    ModificationHistory.Insert(EntityType.SiteDepartment, String.Format("Site: '{0}'", desc), ModificationAction.Modified, Application.CurrentUserInfo.UserID, changeDetails, site.ID, site.ID)
                    NotificationManager.CreateNotificationsByTrigger(NotificationTrigger.DepartmentSetup_ModifySite, oldDesc)
                End If
            Else
                Dim newSite As SiteDepartment = SiteDepartment.InsertSite(desc, addr, isActive, timeZoneID)
                ModificationHistory.Insert(EntityType.SiteDepartment, String.Format("Site: '{0}'", desc), Application.CurrentUserInfo.UserID, newSite.ID, newSite.ID)
                NotificationManager.CreateNotificationsByTrigger(NotificationTrigger.DepartmentSetup_CreateSite, desc)
            End If
            ViewData("SiteList") = SiteDepartmentManager.GetTreeFor(Application.CurrentUserInfo, filter)
            Return PartialView("~/Views/SitesDepartmentSetup/Controls/SiteGrid.ascx")
        End Function

        <HttpPost()> _
        Function SaveDepartment(ByVal siteID As Integer, ByVal id As Integer, ByVal desc As String, ByVal addr As String, ByVal isActive As Boolean, ByVal filter As String) As ActionResult

            Dim location As SiteDepartment
            If id > 0 Then
                Dim changeDetails As New List(Of String)
                location = SiteDepartment.GetByPrimaryKey(id)
                Dim oldDesc As String = location.Description
                If location IsNot Nothing Then
                    If location.Description <> desc Then
                        changeDetails.Add(String.Format(Constants.UI.FIELD_MODIFICATION_TEMPLATE, "Name", location.Description, desc))
                    End If
                    If location.IsActive <> isActive Then
                        changeDetails.Add(String.Format(Constants.UI.FIELD_MODIFICATION_TEMPLATE, "Status", location.IsActive.ToString(), isActive.ToString()))
                    End If
                    If location.Address <> addr Then
                        changeDetails.Add(String.Format(Constants.UI.FIELD_MODIFICATION_TEMPLATE, "Address", location.Address, addr))
                    End If

                    location.Description = desc
                    location.IsActive = isActive
                    location.Address = addr
                    location.Save()
                    ModificationHistory.Insert(EntityType.SiteDepartment, String.Format("Department: '{0}'", desc), ModificationAction.Modified, Application.CurrentUserInfo.UserID, changeDetails, location.ID, siteID)
                    NotificationManager.CreateNotificationsByTrigger(NotificationTrigger.DepartmentSetup_ModifyDepartment, oldDesc)
                End If
            Else
                Dim newDepartment As SiteDepartment = SiteDepartment.InsertDepartment(siteID, desc, addr, isActive)
                ModificationHistory.Insert(EntityType.SiteDepartment, String.Format("Department: '{0}'", desc), Application.CurrentUserInfo.UserID, newDepartment.ID, siteID)
                NotificationManager.CreateNotificationsByTrigger(NotificationTrigger.DepartmentSetup_CreateDepartment, desc)
            End If

            ViewData("DepartmentList") = SiteDepartmentManager.RetrieveAllDepartmentsBySite(siteID)
            Return PartialView("~/Views/SitesDepartmentSetup/Controls/DepartmentGrid.ascx")
        End Function

        <HttpPost()> _
        Function GetDepartmentsBySiteID(ByVal siteID As Integer) As ActionResult
            ViewData("DepartmentList") = SiteDepartmentManager.RetrieveAllDepartmentsBySite(siteID)
            Return PartialView("~/Views/SitesDepartmentSetup/Controls/DepartmentGrid.ascx")
        End Function


        <HttpPost()> _
        Function GetDepartmentDDBySiteID(ByVal siteID As Integer) As ActionResult
            ViewData("Department") = New SelectList(SiteDepartmentManager.RetrieveAllDepartmentsBySite(siteID), "ID", "Description")
            Return PartialView("~/Views/SitesDepartmentSetup/Controls/DepartmentDropDown.ascx")
        End Function
#End Region
    End Class
End Namespace